
/**
 * Storage Helper
 * 
 * Released under Apache License, Version 2.0.
 */

package bsp.android;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.os.Environment;
import bsp.helper.FileHelper;

public class StorageHelper
{	
	protected StorageHelper()
	{
		/* empty by design */
	}
	
	public static boolean isExternalStorageMounted()
	{
		return Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
	}
	
	public static boolean isExternalStorageWritable()
	{
		boolean readonly = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED_READ_ONLY);
		return !readonly;
	}
	
	/*
	public static void externalMedia()
	{
		boolean mExternalStorageAvailable = false;
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();
	
		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    mExternalStorageAvailable = true;
		    mExternalStorageWriteable = false;
		} else {
		    // Something else is wrong. It may be one of many other states, but all we need
		    //  to know is we can neither read nor write
		    mExternalStorageAvailable = mExternalStorageWriteable = false;
		}
	}
	*/

	/**
     * Get a handle to the "data" directory under "Android" directory, on the
     * SD card. If the directory hierarchy does not exist, it is created.
	 */
	public static File getHandleToDataDirectory()
	{
		/*
		int version = Misc.getVersion();
		if ( version <= 7 ) {
			path = Environment.getExternalStorageDirectory();
		} else if ( version > 7 ) {
			path = getExternalFilesDir(null);
		}
		*/

		
		File path = Environment.getExternalStorageDirectory();
		File androidDir = new File(path, "Android");
		if ( androidDir.exists() == false ) {
			androidDir.mkdir();
		}
		File dataDir = new File(androidDir, "data");
		if ( dataDir.exists() == false ) {
			dataDir.mkdir();
		}
		
		return dataDir;
	}

	/**
     * Get a handle to a named directory under "Android/data" directory
     * on the SD card. If the directory hierarchy does not exist, it is created.
	 */
	public static File getHandleToMyDataDirectory(String dirName)
	{
		File dataDir = getHandleToDataDirectory();
		File myDataDir = new File(dataDir, dirName);
		if ( myDataDir.exists() == false ) {
			myDataDir.mkdir();
		}
		
		return myDataDir;
	}
	
	/**
     * Get a handle to a named application specific directory under "Android/data"
     * directory on the SD card. If the directory hierarchy does not exist,
     * it is created.
	 */
	public static File getHandleToAppDirectoryInMyDataDirectory(String dirName, String appName)
	{
		File dataDir = getHandleToDataDirectory();
		File myDataDir = new File(dataDir, dirName);
		File appDataDir = null;
		if ( myDataDir.exists() == false ) {
			myDataDir.mkdir();
		}
		appDataDir = new File(myDataDir, appName);
		if ( appDataDir.exists() == false ) {
			appDataDir.mkdir();
		}
		
		return appDataDir;
	}
	
	/**
	 * Copy file from assets directory to my directory on SD card.
	 * 
	 * @param context
	 * @param source
	 * @param dataDir
	 * @param destination
	 * @return
	 */
	public static boolean copyFileFromAssetsToMyDataDirectory(Context context, String sourceName, String dataDirName, String filename)
	{
		boolean retVal = false;
		
		try {
			File dataPath = getHandleToMyDataDirectory(dataDirName);
			File dataFile = new File(dataPath, filename);
			InputStream is = context.getAssets().open(sourceName);
			retVal = FileHelper.copyFile(is, dataFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return retVal;
	}
	
	/**
	 * Copy file from assets directory to application directory on SD card.
	 * 
	 * @param context
	 * @param source
	 * @param dataDir
	 * @param destination
	 * @return
	 */
	public static boolean copyFileFromAssetsToApplicationDirectory(Context context, String sourceName, String dataDirName, String appDirName, String filename)
	{
		boolean retVal = false;
		
		try {
			File dataPath = getHandleToMyDataDirectory(dataDirName);
			File appPath = new File(dataPath, appDirName);
			File dataFile = new File(appPath, filename);
			InputStream is = context.getAssets().open(sourceName);
			retVal = FileHelper.copyFile(is, dataFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return retVal;
	}
}

/* end */
